#!/bin/sh
# PCP QA Test No. 948
# pmlogger local context functionality
#
# Copyright (c) 2015 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    cd $here
    _service pcp start 2>&1 | _filter_pcp_start
    _wait_for_pmcd
    _restore_auto_restart pmcd
    _wait_for_pmlogger
    _restore_auto_restart pmlogger
    $sudo rm -rf $tmp.*
}

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd
_stop_auto_restart pmlogger

cat <<End-of-File >$tmp.config
log mandatory on 1 sec {
    kernel.all.hz
    hinv.pagesize
    sampledso.long.million
}
End-of-File

_filter_kernel()
{
    sed \
	-e "s/value $hertz$/value HZ/g" \
	-e '/kernel.all.hz/s/[0-9][0-9.]* (/PMID (/' \
	-e "s/value $pagesz$/value PAGESZ/g" \
	-e '/hinv.pagesize/s/[0-9][0-9.]* (/PMID (/' \
	-e 's/TIMESTAMP  /              /g' \
    #end
}

_filter_usage()
{
    sed -e '/^Options:/{
p
s/.*/.../
q
}'
}

# Unable to connect to pmlogger pid 28124 at local:: Connection refused
# Connected to pmlogger pid 25625 at local:
_filter_pmlc()
{
    sed \
	-e "/^Unable/s/ $pid / PID /" \
	-e "/^Connected/s/ $pid / PID /" \
    # end
}

# real QA test starts here
echo "=== error cases ===" | tee -a $seq.full
rm -f $tmp.0 $tmp.meta $tmp.index $tmp.log
pmlogger -c /dev/null -o -h localhost $tmp -l $tmp.log 2>&1 | _filter_usage
[ -f $tmp.log ] && cat $tmp.log >>$seq.full

echo | tee -a $seq.full
echo "=== linger tests ===" | tee -a $seq.full
echo "--- expect error from pmlc ---" | tee -a $seq.full
rm -f $tmp.0 $tmp.meta $tmp.index $tmp.log
pmlogger -c /dev/null -o -l $tmp.log $tmp &
pid=$!
echo "pid=$pid" >>$seq.full
cat <<End-of-File >$tmp.pmlc
quit
End-of-File
sleep 1
pmlc -e <$tmp.pmlc $pid 2>&1 | _filter_pmlc
if ps -p $pid >/dev/null
then
    echo "Error: pmlogger still alive!"
    kill -TERM $pid
fi
[ -f $tmp.log ] && cat $tmp.log >>$seq.full

echo "--- expect success from pmlc ---" | tee -a $seq.full
rm -f $tmp.0 $tmp.meta $tmp.index $tmp.log
$sudo_local_ctx pmlogger -c /dev/null -L -o -l $tmp.log $tmp &
pid=$!
echo "pid=$pid" >>$seq.full
sleep 1
$sudo_local_ctx pmlc -e <$tmp.pmlc $pid 2>&1 | _filter_pmlc
if ps -p $pid >/dev/null
then
    $sudo_local_ctx kill -TERM $pid
else
    echo "Error: pmlogger has died!"
fi
[ -f $tmp.log ] && cat $tmp.log >>$seq.full

echo | tee -a $seq.full
echo "=== OK case ===" | tee -a $seq.full
hertz=`pmprobe -v kernel.all.hz | $PCP_AWK_PROG '{ print $3 }'`
pagesz=`pmprobe -v hinv.pagesize | $PCP_AWK_PROG '{ print $3 }'`

# ensure pmcd is unavailable while this test is running
_service pcp stop | _filter_pcp_stop
$sudo_local_ctx rm -f $tmp.0 $tmp.meta $tmp.index $tmp.log
pmlogger -c $tmp.config -l $tmp.log -T 5 --local-PMDA -K add,30,$PCP_PMDAS_DIR/sample/pmda_sample.$DSO_SUFFIX,sample_init $tmp >$tmp.err 2>&1

_filter_pmlogger_log <$tmp.log

echo "verifying the archive"
pmlogcheck $tmp

# expect 4, 5 or 6 sample lines for each metric
echo PAGESZ samples:
pmdumplog $tmp \
	| _filter_pmdumplog \
	| grep 'hinv.pagesize' \
	| _filter_kernel \
	| sed -e 5d -e 6d
echo HZ samples:
pmdumplog $tmp \
	| _filter_pmdumplog \
	| grep 'kernel.all.hz' \
	| _filter_kernel \
	| sed -e 5d -e 6d
echo MILLION samples:
pmdumplog $tmp \
	| _filter_pmdumplog \
	| grep 'sampledso.long.million' \
	| _filter_kernel \
	| sed -e 5d -e 6d

echo "Full log dump:" >> $seq.full
pmdumplog $tmp >> $seq.full

# success, all done
status=0
exit
